﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SixthClassAssignment.DAL;

namespace SixthClassAssignment{
    public partial class SchWindow : Form{
        private int _selectIndex = -1;
        private void InitGrip(){
            dataGrid .SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
        private void ShowSchools(){
            List<School> schools = SqlDao.SelectSchoolList();
            schools.Add(new School("", "", ""));
            dataGrid.DataSource = schools;
            dataGrid.Columns["No"].HeaderText = "编号";
            dataGrid.Columns["Name"].HeaderText = "校名";
            dataGrid.Columns["Location"].HeaderText = "位置";
        }
        
        private void DataGrid_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e){
            if (e.Button==MouseButtons.Right) {
                if (e.RowIndex>=0) {
                    _selectIndex = e.RowIndex;
                    dataGrid.ClearSelection();//清空选择项
                    dataGrid.Rows[e.RowIndex].Selected = true;//获取被选中行索引
                    dataGrid.CurrentCell = dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    contexMenuStrip.Show(MousePosition.X, MousePosition.Y);//弹出操作菜单
                }
            }
        }

        private void Delete_Click(object sender,EventArgs e){
            var no = dataGrid.Rows[_selectIndex].Cells[0].Value.ToString();
            if (!"".Equals(no)) {
                var count = SqlDao.DeleteSchoolByNo(no);
                if (count == 1) {
                    ShowSchools();
                    SqlDao.InsertLog(new Log(DateTime.Now.ToString(), "delete school: " + no));
                }
            }
        }

        private void Insert_Click(object sender, EventArgs e){
            if (_selectIndex == dataGrid.Rows.Count-1) {
                var cells = dataGrid.Rows[_selectIndex].Cells;
                var no = cells[0].Value.ToString();
                var name = cells[1].Value.ToString();
                var location = cells[2].Value.ToString();
                if (!"".Equals(no)) {
                    School sch = new School(no, name, location);
                    int count = SqlDao.InsertSchool(sch);
                    if (count == 1) {
                        ShowSchools();
                        SqlDao.InsertLog(new Log(DateTime.Now.ToString(), "insert school" + sch));
                    }
                }
            }
        }

        private void Update_Click(object sender, EventArgs e){
            if (_selectIndex != dataGrid.Rows.Count-1) {
                var cells = dataGrid.Rows[_selectIndex].Cells;
                var no = cells[0].Value.ToString();
                var name = cells[1].Value.ToString();
                var location = cells[2].Value.ToString();
                if (!"".Equals(no)) {
                    School sch = new School(no, name,location);
                    int count = SqlDao.UpdateSchool(sch);
                    if (count == 1) {
                        ShowSchools();
                        SqlDao.InsertLog(new Log(DateTime.Now.ToString(), "update school" + sch));

                    }
                }
            }
        }
        private void Form_Closing(object sender, FormClosingEventArgs e){
            Hide();
            var window = new FunctionWindow("root");
            window.Show();
        }

        private void AddEvent(){
            dataGrid.CellMouseClick += DataGrid_CellMouseUp;
            deleteToolStripMenuItem.Click += Delete_Click;
            insertToolStripMenuItem.Click += Insert_Click;
            updateToolStripMenuItem.Click += Update_Click;
            this.FormClosing += Form_Closing;
        }
        
        public SchWindow(){
            InitializeComponent();
            InitGrip();
            ShowSchools();
            AddEvent();
        }
    }
}